package cate.game.play.config.camp;

import cate.common.table.camp.CampAttrBaseRow;
import cate.common.table.d.GDCamp;
import cate.game.play.support.PlayBuildContext;
import cate.game.table.TableManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CampAttrCfgHolder {

	private Map<Integer, CampAttrCfg> map = new HashMap<>();

	private List<CampAttrCfg> list = new ArrayList<>();

	public CampAttrCfg get(int id) {
		return map.get(id);
	}

	public List<CampAttrCfg> getList(){
		return list;
	}

	public void init(TableManager table) {
		map.clear();
		list.clear();
		table.campAttr.base.getList().forEach(this::create);
	}

	public void init(PlayBuildContext context) {
		map.clear();
		list.clear();
		context.campAttrExcel().base.getList().forEach(this::create);
	}

	private void create(CampAttrBaseRow row){
		CampAttrCfg cfg = new CampAttrCfg().init(row);
		map.put(cfg.id, cfg);
		list.add(cfg);
	}

	/** 找到全阵营的情形 */
	public CampAttrCfg findFullCamp() {
		for (CampAttrCfg cfg : list) {
			if (cfg.row.camp == GDCamp.Type.NULL) {
				return cfg;
			}
		}
		return null;
	}

	/** 根据阵营和上阵英雄数来查找 */
	public CampAttrCfg find(byte camp, int num) {
		for (CampAttrCfg cfg : list) {
			if (cfg.row.camp == camp && cfg.row.num == num) {
				return cfg;
			}
		}
		return null;
	}
}
